<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/22 19:15
 */

namespace App\Http\Models;

class StatisticsUserKill extends BaseModel
{
    protected $table = 'statistics_user_kill';
    protected $guarded = [];

    /**
     * 更新秒杀数据
     * @param int $userId
     * @param int $goods_status //0未审 1一审通过 ,2终审通过  3退回 4下架
     * @return mixed
     */
    public static function updateUserKill($userId, $goods_status = 0)
    {
        $model = self::where(['user_id' => $userId])->first();
        if (!$model) {
            $model = new StatisticsUserKill();
            $model->user_id = $userId;
            $model->submission_number = 1;
            $model->wait_number = 1;
        } else {
            if ($goods_status == 0) {
                $model->increment('submission_number');  //
                $model->increment('wait_number');
            } elseif ($goods_status == 2) {
                if ($model->wait_number > 0) {
                    $model->decrement('wait_number');
                }
                $model->increment('conduct_number');
            } elseif ($goods_status == 3) {
                $model->increment('refuse_number');
                $model->decrement('wait_number');
            } else {
                if ($model->conduct_number > 0) {
                    $model->decrement('conduct_number');
                }
            }
        }
        return $model->save();
    }

    /**
     * 增加用户
     * @param $userId 用户id
     * @param $goodsstatus 商品状态  //0未审 1一审通过 ,2终审通过  3退回 4下架
     */
    public static function addUserOnlineGood($userId, $goodsstatus)
    {
        $model = self::where(['user_id' => $userId])->first();
        if (!$model) {
            self::create(['user_id' => $userId]);
            $model = self::where(['user_id' => $userId])->first();
        }
        if ($goodsstatus == 2) {
            $model->increment('all_online_number');
        } elseif ($goodsstatus == 0 || $goodsstatus == 5 || $goodsstatus == 3) {

        } else {
            $model->decrement('all_online_number');
        }
        return $model->save();
    }

}